﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_Login : DataAccess, ILogin
    {
        public EntityLogin GetLoginDetails(string Username,string Password)
        {
            EntityLogin logindetails = new EntityLogin();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Userdetails]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.ONE);
                        SQLServer.AddInParameter(dbCommand, "@username", DbType.String, Username);
                        SQLServer.AddInParameter(dbCommand, "@Password", DbType.String, Password);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return logindetails;
                            }
                            while (OReader.Read())
                            {
                                logindetails.CHChurchid = OReader["CHChurchid"].ToString();
                                logindetails.Churchid = (Guid)OReader["Churchid"];
                                logindetails.IsHeadChurch = OReader["IsHeadChurch"].ToString();
                                logindetails.IsSuperAdmin = (bool)OReader["IsSuperAdmin"];
                                logindetails.LandingAction = OReader["ActionName"].ToString();
                                logindetails.LandingController = OReader["Controllername"].ToString();
                                logindetails.CultureId = 0;
                                logindetails.Lastlogin = (DateTime)OReader["Lastlogin"];
                                logindetails.Name = OReader["Name"].ToString();
                                logindetails.PartitionId = OReader["PartitionId"].ToString();
                                logindetails.Userid = OReader["Userid"].ToString();
                                logindetails.UserTypeid = OReader["UserTypeid"].ToString();
                                logindetails.UserTypeName = OReader["UserTypeName"].ToString();
                                logindetails.Area = OReader["area"].ToString();
                            }
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                logindetails.UserSettings = new EntityUserSettings();
                                logindetails.UserSettings.CountryId = (Guid)OReader["CountryId"];
                                logindetails.UserSettings.StateId = (Guid)OReader["StateId"];
                                logindetails.UserSettings.UserPicture = OReader["UserPicture"].ToString();
                                logindetails.UserSettings.DetaultAddress1 = OReader["DetaultAddress1"].ToString();
                                logindetails.UserSettings.DetaultAddress2 = OReader["DetaultAddress2"].ToString();
                                logindetails.UserSettings.DetaultPinCode = OReader["DetaultPinCode"].ToString();
                                logindetails.UserSettings.LanguageId = (Guid)OReader["LanguageId"];

                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return logindetails;
        }
    }
    
}
